﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.IO;

namespace Server
{
    class Program
    {
        static void Main(string[] args)
        {
            // 服务器基址   
            Uri baseAddress = new Uri("http://localhost:1378/services");
            // 声明服务器主机   
            using (ServiceHost host = new ServiceHost(typeof(MyService), baseAddress))
            {
                // 添加绑定和终结点   
                BasicHttpBinding binding = new BasicHttpBinding();
                // 启用流模式   
                binding.TransferMode = TransferMode.Streamed;
                binding.MaxBufferSize = 1024;
                // 接收消息的最大范围为500M   
                binding.MaxReceivedMessageSize = 500 * 1024 * 1024;
                host.AddServiceEndpoint(typeof(IService), binding, "/test");
                // 添加服务描述   
                host.Description.Behaviors.Add(new ServiceMetadataBehavior { HttpGetEnabled = true });
                try
                {
                    // 打开服务   
                    host.Open();
                    Console.WriteLine("服务已启动。");
                    Console.ReadLine();
                    host.Close();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                Console.ReadKey();
            }
        }
    }

    [ServiceContract(Namespace = "MyNamespace")]
    public interface IService
    {
        [OperationContract]
        ResultMessage UpLoadFile(TransferFileMessage tMsg);
    }


    public class MyService : IService
    {

        public ResultMessage UpLoadFile(TransferFileMessage tMsg)
        {
            ResultMessage rMsg = new ResultMessage();
            if (tMsg == null || tMsg.File_Stream == null)
            {
                rMsg.ErrorMessage = "传入的参数无效。";
                rMsg.IsSuccessed = false;
                return rMsg;
            }
            try
            {
                using (FileStream outputStream = new FileStream(tMsg.File_Name, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    // 我们不用对两个流对象进行读写，只要复制流就OK   
                    tMsg.File_Stream.CopyTo(outputStream);
                    outputStream.Flush();
                    rMsg.IsSuccessed = true;
                    Console.WriteLine("在{0}接收到客户端发送的流，已保存到{1}。", DateTime.Now.ToLongTimeString(), tMsg.File_Name);
                }
            }
            catch (Exception ex)
            {
                rMsg.IsSuccessed = false;
                rMsg.ErrorMessage = ex.Message;
            }
            return rMsg;
        }
    }


    [MessageContract]
    public class TransferFileMessage
    {
        [MessageHeader]
        public string File_Name; //文件名   
        [MessageBodyMember]
        public Stream File_Stream; //文件流   
    }
    [MessageContract]
    public class ResultMessage
    {
        [MessageHeader]
        public string ErrorMessage;
        [MessageBodyMember]
        public bool IsSuccessed;
    }

}
